package cn.microven.mindmap.oprations;

import cn.microven.mindmap.components.node.AbstractMapNode;
import cn.microven.mindmap.components.node.SelectedFontInformation;
import cn.microven.mindmap.struture.MindMap;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.awt.*;
import java.net.URL;
import java.util.List;
import java.util.*;

public class FontChooseOperation implements Initializable{
    @FXML
    private Label font;//字体类型标签
    @FXML
    private Label type;//字体粗斜标签
    @FXML
    private Label size;//字体大小标签
    @FXML
    private ListView<Text> fontList;
    @FXML
    private ListView<Text> typeList;
    @FXML
    private ListView<String> sizeList;
    @FXML
    public ColorPicker fontColor;
    //private ComboBox<HBox> colorList;
    @FXML
    private Button ok;
    @FXML
    private Button cancel;
    @FXML
    private Label sample;//字体例子标签
    private Map<String, Color> colorMap;
    private Map<String, Integer> sizeMap;
    private FontWeight weiStyle;
    private FontPosture posStyle;
    private SelectedFontInformation selectedFont;

    private String OldType;
    private String NewType;
    private Color OldColor;
    private Color NewColor;
    private  int NewSize;
    private  int OldSize;
    private AbstractMapNode node;

    public FontChooseOperation() {
        this.weiStyle = FontWeight.NORMAL;
        this.posStyle = FontPosture.REGULAR;
        this.selectedFont = null;
    }

    public void initialize(URL location, ResourceBundle resources) {
        //获取字体类型
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        //返回一个数组，其中包含针对指定语言环境本地化的此 GraphicsEnvironment中所有字体系列的名称。
        String[] fontNames = ge.getAvailableFontFamilyNames();
        List<Text> textList = new ArrayList(fontNames.length);

        //将字体类型全部添加到Text类型的textList中，并将Text中的文字类型设置为名称对应的
        for(int i = 0; i < fontNames.length; ++i) {
            textList.add(new Text(fontNames[i]));
            ((Text)textList.get(i)).setFont(Font.font(fontNames[i]));
        }
        //添加字体进入ListView
        this.fontList.setItems(FXCollections.observableArrayList(textList));
        //ListView中默认选择第一个类型
        this.fontList.getSelectionModel().selectFirst();
        //this.font.setText(((Text)this.fontList.getSelectionModel().getSelectedItem()).getText());

        //事件监听
        this.fontList.getSelectionModel().selectedItemProperty().addListener((event) -> {
            //将字体类型标签中的字体设置成所选字体类型
            this.font.setText(((Text)this.fontList.getSelectionModel().getSelectedItem()).getText());
            //将字体例子标签中的字体设置成所选字体类型
            this.sample.setFont(Font.font(((Text)this.fontList.getSelectionModel().getSelectedItem()).getText(), this.weiStyle, this.posStyle, (double)(Integer)this.sizeMap.get(this.sizeList.getSelectionModel().getSelectedItem())));
        });

        Text normal_regular = new Text("常规");
        Text bold_regular = new Text("加粗");
        Text normal_italic = new Text("斜体");
        Text bold_italic = new Text("粗斜体");
        normal_regular.setFont(Font.font(Font.getDefault().getName(), FontWeight.NORMAL, FontPosture.REGULAR, Font.getDefault().getSize()));
        bold_regular.setFont(Font.font(Font.getDefault().getName(), FontWeight.BOLD, FontPosture.REGULAR, Font.getDefault().getSize()));
        normal_italic.setFont(Font.font(Font.getDefault().getName(), FontWeight.NORMAL, FontPosture.ITALIC, Font.getDefault().getSize()));
        bold_italic.setFont(Font.font(Font.getDefault().getName(), FontWeight.BOLD, FontPosture.ITALIC, Font.getDefault().getSize()));

        this.typeList.setItems(FXCollections.observableArrayList(new Text[]{normal_regular, bold_regular, normal_italic, bold_italic}));
        this.typeList.getSelectionModel().selectFirst();
        this.type.setText(((Text)this.typeList.getSelectionModel().getSelectedItem()).getText());
        this.typeList.getSelectionModel().selectedItemProperty().addListener((event) -> {
            String value = ((Text)this.typeList.getSelectionModel().getSelectedItem()).getText();
            if (value.equals("常规")) {
                this.weiStyle = FontWeight.NORMAL;
                this.posStyle = FontPosture.REGULAR;
            } else if (value.equals("加粗")) {
                this.weiStyle = FontWeight.BOLD;
                this.posStyle = FontPosture.REGULAR;
            } else if (value.equals("斜体")) {
                this.weiStyle = FontWeight.NORMAL;
                this.posStyle = FontPosture.ITALIC;
            } else if (value.equals("粗斜体")) {
                this.weiStyle = FontWeight.BOLD;
                this.posStyle = FontPosture.ITALIC;
            }

            this.type.setText(value);
            //this.sample.setFont(Font.font(((Text)this.fontList.getSelectionModel().getSelectedItem()).getText(), this.weiStyle, this.posStyle, (double)(Integer)this.sizeMap.get(this.sizeList.getSelectionModel().getSelectedItem())));
        });
        String[] sizeStr = new String[]{"8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72"};
        int[] sizeValue = new int[]{8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72};
        this.sizeList.setItems(FXCollections.observableArrayList(sizeStr));
        this.sizeMap = new HashMap();

        for(int i = 0; i < this.sizeList.getItems().size(); i++) {
            this.sizeMap.put(sizeStr[i], sizeValue[i]);
        }

        this.sizeList.getSelectionModel().select(4);
        this.size.setText((String)this.sizeList.getSelectionModel().getSelectedItem());
        this.sizeList.getSelectionModel().selectedItemProperty().addListener((event) -> {
            this.size.setText((String)this.sizeList.getSelectionModel().getSelectedItem());
            this.sample.setFont(Font.font(((Text)this.fontList.getSelectionModel().getSelectedItem()).getText(), this.weiStyle, this.posStyle, (double)(Integer)this.sizeMap.get(this.sizeList.getSelectionModel().getSelectedItem())));
        });
        /*String[] colorStr = new String[]{"黑色", "蓝色", "青色", "深灰", "灰色", "绿色", "浅灰", "洋红", "桔黄", "粉红", "红色", "白色", "黄色"};
        Color[] colorValue = new Color[]{Color.BLACK, Color.BLUE, Color.CYAN, Color.DARKGRAY, Color.GRAY, Color.GREEN, Color.LIGHTGRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW};
        List<HBox> hboxList = new ArrayList(colorValue.length);

        int i;
        for(i = 0; i < colorValue.length; ++i) {
            Rectangle rect = new Rectangle(80.0D, 15.0D);
            rect.setFill(colorValue[i]);
            HBox hBox = new HBox();
            Label label = new Label(colorStr[i]);
            label.setTextFill(colorValue[0]);
            hBox.setSpacing(5.0D);
            hBox.getChildren().addAll(new Node[]{label, rect});
            hboxList.add(hBox);
        }*/

        /*this.colorList.setItems(FXCollections.observableArrayList(hboxList));
        this.colorList.getSelectionModel().select(0);
        this.colorMap = new HashMap();

        for(i = 0; i < this.colorList.getItems().size(); ++i) {
            this.colorMap.put(colorStr[i], colorValue[i]);
        }

        this.colorList.getSelectionModel().selectedItemProperty().addListener((event) -> {
            ((HBox)this.colorList.getSelectionModel().getSelectedItem()).getChildren().forEach((node) -> {
                if (node.getClass().equals(Label.class)) {
                    Label label = (Label)node;
                    this.sample.setTextFill(this.colorMap.get(label.getText()));
                }

            });
        });*/
        //this.sample.setFont(Font.font(((Text)this.fontList.getSelectionModel().getSelectedItem()).getText(), this.weiStyle, this.posStyle, (double)(Integer)this.sizeMap.get(this.sizeList.getSelectionModel().getSelectedItem())));
        this.fontColor.valueProperty().addListener(((observableValue, color, t1) -> {
            this.sample.setTextFill(fontColor.getValue());
            //this.sample.setFont(Font.font(((Text)this.fontList.getSelectionModel().getSelectedItem()).getText(), this.weiStyle, this.posStyle, (double)(Integer)this.sizeMap.get(this.sizeList.getSelectionModel().getSelectedItem())));
        }));



        this.ok.setOnAction((event) -> {
            /*this.selectedFont = new SelectedFontInformation();
            this.selectedFont.setFont(Font.font(((Text)this.fontList.getSelectionModel().getSelectedItem()).getText(), this.weiStyle, this.posStyle, (double)(Integer)this.sizeMap.get(this.sizeList.getSelectionModel().getSelectedItem())));
            ((HBox)this.colorList.getSelectionModel().getSelectedItem()).getChildren().forEach((node) -> {
                if (node.getClass().equals(Rectangle.class)) {
                    Rectangle rect = (Rectangle)node;
                    this.selectedFont.setColor((Color)rect.getFill());
                }
            });*/
            node = MindMap.getInstance().getSelectedNode();
            OldColor = node.getFontColor();
            OldSize = node.getFontSize();
            OldType = node.getFontFamily();
            NewColor = fontColor.getValue();
            NewSize = Integer.parseInt(this.sizeList.getSelectionModel().selectedItemProperty().getValue());
            NewType = ((Text)this.fontList.getSelectionModel().getSelectedItem()).getText();
            /*node.setFontFamily(NewType);
            node.setFontColor(NewColor);
            node.setFontSize(NewSize);*/
            new  ChangeFontTypeOperation(NewType,OldType,node).start();
            new ChangeFontSizeOperation(OldSize,NewSize,node).start();
            new ChangeFontColorOperation(OldColor,NewColor,node).start();
            ((Stage)this.cancel.getScene().getWindow()).hide();
        });
        this.cancel.setOnAction((event) -> {
            ((Stage)this.cancel.getScene().getWindow()).hide();
        });

    }


}
